package oog.product.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oog.product.ReqUtil;
import oog.product.Util;
import oog.product.biz.SessionBiz;
import oog.product.entity.MyUser;
import oog.product.form.LogonForm;

/**
 * Logon Controller.
 */
public class LogonServlet extends BaseServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                          IOException {
        super.service(request, response);
        LogonForm form = null;
        String nextPage = PAGE_LOGON; // default, re-display the logon page
        
        try {
            LOG.debug("doPost.START");
            form = (LogonForm) ReqUtil.parseRequest(request, LogonForm.class);
            session = request.getSession();
            if (form != null)
            {
                String cmd = form.getCmd();
    
                if ("Logon".equals(cmd)) {
                    // 1. Get the session
                    HttpSession session = request.getSession();
    
                    // 2. Data validation
                    List<String> errorList = validate(form);
                    if (Util.isNNandNB(errorList)) {
                        request.setAttribute(KEY_ERRORLIST, errorList);
                    } else {
                        processLogon(form, request, response);
                     // 4. Forward to Product page
                        nextPage = PAGE_PRODUCT_SEARCH;
                    }
                } else { // Initial screen
                    if (session.getAttribute(KEY_USER) != null) {
                        nextPage = PAGE_PRODUCT_SEARCH;
                    }
                }
                // Keep the logon form in the request
                request.setAttribute(KEY_FORM, form);
            }
            request.getRequestDispatcher(nextPage).forward(request, response);
            LOG.info("doPost.END");
        } catch (Throwable th) {
            LOG.error("Logon", th);
            request.getRequestDispatcher(PAGE_ERROR).forward(request, response);
        }
    }

    /**
     * [Give the description for method].
     * @param form 
     * @param request
     * @param response
     * @throws IOException 
     * @throws ServletException 
     */
    private void processLogon(LogonForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 3. Save the logon user into the session
        // logon successful
        
        MyUser logonUser = SessionBiz.getUser(form.getEmail());
        if (logonUser == null) {
            logonUser = new MyUser(form.getEmail());
        }
        session.setAttribute(KEY_USER, logonUser);
    }

    private List<String> validate(LogonForm form) {
        return null;
    }
}
